<?php

/**
 * Controller.php
 *
 * Holds abstract class Controller
 *
 * @package Application
 * @subpackage Controllers
 */

/**
 * Controller
 *
 * Application specific base controller
 *
 * @package Application
 * @subpackage Controllers
 */
abstract class Controller extends Mvc_Controller_Base {

	//	protected $content_type_header = 'application/xhtml+xml; charset=ISO-8859-1';
	protected $content_type_header = 'text/html; charset=ISO-8859-1';

	protected $content_type_header_list = array(
		"Smarty"	=> "text/html; charset=ISO-8859-1",
		"PureJSON"	=> "application/json"
	);

	/**
	 * Initializes page
	 *
	 * @return Controller (fluent interface)
	 */
	public function init() {
		// Set output type
		$this->setView($this->_view);

		return $this;
	}

	protected function redirect($module = null,$controller = null,$action = null, $params = ""){
		$config = new Config();
		$config->import(APP . 'config/routes.php');

		list($_m, $_c, $_a) = explode("/", $config->get("_default"));

		$module = null === $module ? $_m : $module;
		$controller = null === $controller ? $_c : $controller;
		$action = null === $action ? $_a : $action;

		$redir = "location: /{$module}/{$controller}/{$action}/{$params}";

		die(header($redir));
	}

	protected function setView($view){
		$this->_view = $view;

		if( isset($this->content_type_header_list[$view]) ){
			$this->content_type_header = $this->content_type_header_list[$view];
		}

		$this->getResponse()->setHeader('Content-type', $this->content_type_header);
	}


	/**
	 * Simple helper function
	 *
	 * Set view to PureJSON, unset all previously assigned variables
	 */
	protected function setPureJSON(){
		//Make sure all previously assigned variables are unset,
		//as we don't want specific user information to be JSON encoded.
		//The controller should provide all relevant JSON data after calling unSetAll();
		$this->unSetAll();
		$this->setView("PureJSON");
		$this->success = false;
	}
}
